package cn.cihon.ubi.qorosauto.dao.hadoop;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;

public class DataToHdfs {


	private static String dst = "hdfs://123.57.54.113:9000/hdfsfile/tmp/input/1.txt";
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		uploadToHdfs();
		System.out.println("upload结束");
		readFromHdfs();
		System.out.println("read结束");
		//appendToHdfs();
		//appendToFlyData();
	}
	

	 
//	 private static void appendToFlyData() throws FileNotFoundException,IOException {
//		  String dst = "hdfs://master:9000/hadoop/hadoopfile/flydata/flydata.txt";  
//		  Configuration conf = new Configuration();  
//		  FileSystem fs = FileSystem.get(URI.create(dst), conf);  
//		  FSDataOutputStream out = fs.append(new Path(dst));
//
//		 // List<Map<String, String>> list = TestInputData.getData();
//		  List<Map<String, String>> list = TestInputData.getData();
//		  for(Map<String, String> map :list){
//			  
//			  String rowId = map.get("rowId");
//			  String flyId = map.get("flyId");
//			  String flyName = map.get("flyName");
//			  String paramId = map.get("paramId");
//			  String paramName = map.get("paramName");
//			  String paramCode = map.get("paramCode");
//			  String xyId = map.get("xyId");
//			  String xName = map.get("xName");
//			  String yName = map.get("yName");
//			  String xvalue = map.get("xvalue");
//			  String yvalue = map.get("yvalue");
//			  
//			  String data = rowId+" "+flyId+" "+flyName+" "+paramId+" "+paramName+" "+paramCode+" "+xyId+" "+xName+" "+yName+" "+xvalue+" "+yvalue+"\n";
//			  
//			  
//			int readLen = data.getBytes().length;
//
//			  out.write(data.getBytes(),0,readLen);	
//
//			  
//			  
//			  
//		  }
//	
//		  out.close();
//		  fs.close();
//		 }	 
	
	 private static void uploadToHdfs() throws FileNotFoundException,IOException {
		  String localSrc = "c:/hdfsTest/1.txt";
		  InputStream in = new BufferedInputStream(new FileInputStream(localSrc));
		  Configuration conf = new Configuration();
		  
		  FileSystem fs = FileSystem.get(URI.create(dst), conf);
		  OutputStream out = fs.create(new Path(dst), new Progressable() {
		   public void progress() {
		    System.out.print(".");
		   }
		  });
		  IOUtils.copyBytes(in, out, 4096, true);
		 }
//	 
//	
	 private static void readFromHdfs() throws FileNotFoundException,IOException {
		  Configuration conf = new Configuration();  
		  FileSystem fs = FileSystem.get(URI.create(dst), conf);
		  FSDataInputStream hdfsInStream = fs.open(new Path(dst));
		  
		  OutputStream out = new FileOutputStream("c:/hdfsTest/2.txt"); 
		  byte[] ioBuffer = new byte[1024];
		  int readLen = hdfsInStream.read(ioBuffer);

		  while(-1 != readLen){
		  out.write(ioBuffer, 0, readLen);  
		  readLen = hdfsInStream.read(ioBuffer);
		  }
		  out.close();
		  hdfsInStream.close();
		  fs.close();
		 }
	 

}
